package com.jjs.jcenterdemo.camera2;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;

import com.jjs.jcenterdemo.R;

public class CameraActivity extends AppCompatActivity
{

    SurfaceView surface;
    CameraManager cameraManager;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        surface = (SurfaceView) findViewById(R.id.surface);
        mHandler = new Handler(getMainLooper());
        handlePermission();
    }

    private void handlePermission()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
                    PackageManager.PERMISSION_GRANTED)
            {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.CAMERA}, 32);
            }else
            {
                init();
            }
        }
    }

    boolean mFlashSupportable;
    String mCameraId;

    private void init()
    {
        cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        if(cameraManager == null) return;
        try
        {
            for(String id : cameraManager.getCameraIdList())
            {
                CameraCharacteristics cc = cameraManager.getCameraCharacteristics(id);
                Integer facing = cc.get(CameraCharacteristics.LENS_FACING);
                if(facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT)
                    continue;
                StreamConfigurationMap m = cc.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                if(m == null)
                    continue;
                Boolean b = cc.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
                mFlashSupportable = b != null && b;
                mCameraId = id;
                break;
            }
            tryOpenCamera();
        } catch (CameraAccessException e)
        {
            e.printStackTrace();
        }
    }

    private void tryOpenCamera()
    {
        if(mCameraId != null)
        {
            /*cameraManager.openCamera(mCameraId, new CameraDevice.StateCallback()
            {
                @Override
                public void onOpened(@NonNull CameraDevice camera)
                {

                }

                @Override
                public void onDisconnected(@NonNull CameraDevice camera)
                {

                }

                @Override
                public void onError(@NonNull CameraDevice camera, int error)
                {

                }
            }, );*/
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(PackageManager.PERMISSION_GRANTED == grantResults[0])
        {
            init();
        }
    }
}
